package com.baway.biliclitent.fragment.ZoneType;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.SynopsisFragment_Adapter;
import com.baway.biliclitent.model.DesBean;
import com.baway.biliclitent.util.CommonUtil;
import com.baway.biliclitent.util.OkHttpUtils;
import com.baway.biliclitent.util.UrlUtils;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;



/**
 * Created by dell on 2017/3/23.
 */

public class SynopsisFragment extends Fragment {

    private TextView tv_title;
    private TextView tv_play_time;
    private TextView tv_review_count;
    private TextView tv_description;
    private TextView share_num;
    private TextView coin_num;
    private TextView fav_num;
    private TextView textView;
    private TextView synopsis_name;
    private LinearLayout btn_share;
    private LinearLayout btn_coin;
    private LinearLayout btn_fav;
    private LinearLayout btn_download;
    private ImageView synopsis_image;
    private TagFlowLayout tags_layout;
    private RelativeLayout layout_video_related;
    private RecyclerView recycle;

    private OkHttpUtils httpUtils;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_synopsis,container,false);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_play_time = (TextView) view.findViewById(R.id.tv_play_time);
        tv_review_count = (TextView) view.findViewById(R.id.tv_review_count);
        tv_description = (TextView) view.findViewById(R.id.tv_description);
        share_num = (TextView) view.findViewById(R.id.share_num);
        coin_num = (TextView) view.findViewById(R.id.coin_num);
        fav_num = (TextView) view.findViewById(R.id.fav_num);
        textView = (TextView) view.findViewById(R.id.textView);
        synopsis_name = (TextView) view.findViewById(R.id.synopsis_name);
        btn_share = (LinearLayout) view.findViewById(R.id.btn_share);
        btn_coin = (LinearLayout) view.findViewById(R.id.btn_coin);
        btn_fav = (LinearLayout) view.findViewById(R.id.btn_fav);
        btn_download = (LinearLayout) view.findViewById(R.id.btn_download);

        synopsis_image = (ImageView) view.findViewById(R.id.synopsis_image);
        tags_layout = (TagFlowLayout) view.findViewById(R.id.tags_layout);

        layout_video_related = (RelativeLayout) view.findViewById(R.id.layout_video_related);
        recycle = (RecyclerView) view.findViewById(R.id.recycle);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setTextView();
        httpUtils = OkHttpUtils.getHttpUtils();
        if (CommonUtil.isNetworkAvailable(getActivity()) ){
            getData(UrlUtils.ZONE_DES);
        }else{
            Toast.makeText(getActivity(),"当前没有网络连接",Toast.LENGTH_SHORT).show();
        }
    }
    /**
     *  模拟赋值
     */
    private void setTextView(){
        tv_title.setText("【10月】忍者少女千鸟 / 信长的忍者25");
        tv_play_time.setText("45022");
        tv_review_count.setText("301");
        tv_description.setText("#25");
        share_num.setText("24");
        coin_num.setText("152");
        fav_num.setText("33");
        synopsis_name.setText("TV-TOKYO");
    }
    /**
     *   进行适配
     */
    private void setAdapter(DesBean result){
        List<DesBean.DataBeanHeadline.RelatesBeanHeadline> relates = result.getData().getRelates();
        //进行适配
        SynopsisFragment_Adapter adapter = new SynopsisFragment_Adapter(relates,getActivity());
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recycle.setLayoutManager(layoutManager);
        recycle.setAdapter(adapter);
    }
    /**
     *  进行网络请求
     */
    private void getData(String path) {
        httpUtils.loadDataFromNet(path, DesBean.class, new OkHttpUtils.CallBackListener<DesBean>() {
            @Override
            public void onSuccess(DesBean result) {
                setAdapter(result);
            }

            @Override
            public void onFail() {

            }
        });
    }

}
